package com.ysh.xiaomi.web.servelt;

import com.ysh.xiaomi.pojo.TbGoods;
import com.ysh.xiaomi.service.GoodsService;
import com.ysh.xiaomi.service.impl.GoodsServiceImpl;
import com.ysh.xiaomi.utils.StringConstans;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 袁胜虎 on 2022/8/21 19:39
 */
public class GoodsServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    public String add(HttpServletRequest req, HttpServletResponse resp) {
        String pathName= "";
        String fjName = "";
        FileInputStream fis = null;
        ByteArrayOutputStream out =null;
        TbGoods tbGoods = new TbGoods();

        if (ServletFileUpload.isMultipartContent(req)){
            FileItemFactory fileItemFactory = new DiskFileItemFactory();

            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try{
                List<FileItem> list = servletFileUpload.parseRequest(req);
                Map<String,String> map =new HashMap<>();
                for (FileItem fileItem:list){
                    if (fileItem.isFormField()){
                        map.put(fileItem.getFieldName(),fileItem.getString("UTF-8"));
                    }
                    if (!fileItem.isFormField()){
                        if(fileItem.getName() != null && fileItem.getName() != ""){
                            fjName = fileItem.getName();
                            pathName = pathName+"d:\\IDEA\\xiaomi\\web\\admin\\image\\"+fileItem.getName();
                            fileItem.write(new File(pathName));
                        }
                    }
                }
                if(fjName != ""){
                    fis = new FileInputStream(pathName);
                    out = new  ByteArrayOutputStream();
                    int len = 0;
                    byte[] b = new byte[1024];
                    while ((len = fis.read(b))!=-1){
                        out.write(b,0,len);
                    }
                    byte [] arrImg =out.toByteArray();
                    tbGoods.setPicture(arrImg);
                }



//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//                if(map.get("pubdate") != null){
//                    Date date = simpleDateFormat.parse(map.get("pubdate"));
//                    tbGoods.setPubdate(date);
//                }


                tbGoods.setName(map.get("name"));
                tbGoods.setTypeid(Long.parseLong(map.get("typeid")));
                tbGoods.setPrice(Integer.parseInt(map.get("price")));
                tbGoods.setStar(Integer.parseInt(map.get("star")));
                tbGoods.setIntro(map.get("intro"));

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fis!=null){
                        fis.close();
                    }
                    if(out!=null){
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if(goodsService.addGoods(tbGoods)>0){
            return StringConstans.REDIRECT + "/admin/showGoodsType.jsp";
        }else{
            req.getSession().setAttribute("exp","添加商品失败");
            return StringConstans.FORWARD + "/error/error.jsp";
        }
    }
}
